package com.fastloan.jn.mgt.process.tactics.impl;

import com.fastloan.jn.mgt.process.model.JnAppApproveUser;
import com.fastloan.jn.mgt.process.service.AppApproveService;
import com.fastloan.jn.mgt.process.service.AppApproveUserService;
import com.fastloan.jn.mgt.process.service.ProcessService;
import com.fastloan.jn.mgt.process.service.impl.ApplicationAuditing;
import com.fastloan.jn.mgt.process.tactics.TacticsGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


;import static com.fastloan.jn.mgt.application.basic.enums.ApplicationStatus.APP_REJECT;
import static com.fastloan.jn.mgt.application.basic.enums.ApplicationStatus.RETURN_MANAGER;
import static com.fastloan.jn.mgt.application.basic.enums.ApplicationStatus.UNDER_REVIEW;
import static com.fastloan.jn.mgt.process.enums.AppApproveStatus.*;
import static com.fastloan.jn.mgt.process.enums.TacticsType.USER_AUDITING;

/**
 * Created by chenkai on 2017/3/6.
 */
@Service
public class UserFlow implements TacticsGenerator {

    @Autowired
    private AppApproveUserService appApproveUserService;

    @Autowired
    private AppApproveService appApproveService;

    @Autowired
    private ProcessService processService;

    @Autowired
    private ApplicationAuditing applicationAuditing;

//    @Autowired
//    private RiskCustomerService riskCustomerService;

    @Override
    @Transactional
    public String runTactics(JnAppApproveUser appApproveUser, String applicationId) {
        Integer approvalDecide = appApproveUser.getApprovalConclution();
        JnAppApproveUser approveUserOne = new JnAppApproveUser();
        approveUserOne.setAppApproveId(appApproveUser.getAppApproveId());
        if (approvalDecide == USER_APPROVALCONCLUTION_ADOPT.getValue() ||
                approvalDecide == USER_APPROVALCONCLUTION_CONDITION.getValue()) {
           // OperationLogUtil.addLog(USER_APPROVE, LOAN_APPROVAL, SUCCESS);
            return passed(appApproveUser, approveUserOne);
        }
   //     OperationLogUtil.addLog(USER_APPROVE, LOAN_APPROVAL, FAIL);
        return unApprove(appApproveUser, approveUserOne, approvalDecide, applicationId);
    }

    @Override
    public Integer getTacticsType() {
        return USER_AUDITING.getValue();
    }

    /**
     * 审核通过
     *
     * @param appApproveUser 审批信息
     * @param approveUserOne 临时对象
     * @return 审核id
     */
    private String passed(JnAppApproveUser appApproveUser, JnAppApproveUser approveUserOne) {
        // 审批通过或有条件通过
        appApproveUser.setStatus(STATUS_ADOPT.getValue());
        appApproveUserService.updateByPrimaryKeySelective(appApproveUser);
        List<JnAppApproveUser> appApproveUserList = appApproveUserService.select(approveUserOne);
        appApproveService.insertApproval(appApproveUser, UNDER_REVIEW);
        int count = 0;
        //检测是否有下一个审批节点
        for (JnAppApproveUser approveUser : appApproveUserList) {
            if (null != approveUser.getApprovalConclution() && (approveUser.getApprovalConclution() == USER_APPROVALCONCLUTION_CONDITION.getValue()
                    || approveUser.getApprovalConclution() == USER_APPROVALCONCLUTION_ADOPT.getValue())) {
                count++;
            }
        }
        if (appApproveUserList.size() == count) { //有下一个审批节点则增加下一个节点的审批信息
            return appApproveUser.getAppApproveId();
        }
        return null;
    }

    /**
     * 审核未通过
     *
     * @param appApproveUser 审批信息
     * @param approveUserOne 临时对象
     * @param approvalDecide 意见
     * @param applicationId  进件id
     * @return 审核id
     */
    private String unApprove(JnAppApproveUser appApproveUser, JnAppApproveUser approveUserOne, Integer approvalDecide,
                              String applicationId) {
        approveUserOne.setStatus(USER_STATUS_ONE.getValue());
        List<JnAppApproveUser> appApproveUserList = appApproveUserService.select(approveUserOne);
        appApproveUserService.updateByPrimaryKeySelective(appApproveUser);
        if (!appApproveUserList.isEmpty() && approvalDecide == USER_APPROVALCONCLUTION_BACK.getValue()) { // 2 退回修改
            for (JnAppApproveUser approveUser : appApproveUserList) {
                approveUser.setStatus(STATUS_OTHER_UNTREAD.getValue());
                appApproveUserService.updateByPrimaryKeySelective(approveUser);
            }
            processService.updateAppApprove(appApproveUser.getAppApproveId());
            appApproveService.insertApproval(appApproveUser, RETURN_MANAGER);
        }

        if (!appApproveUserList.isEmpty() && approvalDecide == USER_APPROVALCONCLUTION_OTHER_REJECT.getValue()) { // 4 拒绝
           // riskCustomerService.insertRiskCustomer(applicationId); //插入逾期名单
            for (JnAppApproveUser approveUser : appApproveUserList) {
                approveUser.setStatus(STATUS_OTHER_REJECT.getValue());
                appApproveUserService.updateByPrimaryKeySelective(approveUser);
            }
            appApproveService.insertApproval(appApproveUser, APP_REJECT);
            processService.updateAppApprove(appApproveUser.getAppApproveId());

//            try {
//                FunctionDispatcher.getInstance().executeEnd(applicationAuditing, applicationId, false);
//            } catch (Exception e) {
//                e.printStackTrace();
//                LogTemplate.info(this.getClass(), "审批流程未正确结束", e.getMessage()); //当用户拒绝时审批流程结束
//                throw new HandleOrgException("审批流程未正确结束");
//            }
        }
        return null;
    }

}
